
package com.dogsong.liteflow.editor.controller;

import com.dogsong.liteflow.editor.bean.CommonResult;
import com.dogsong.liteflow.editor.slot.DataContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;

import javax.annotation.Resource;

/**
 * 流程执行Controller
 *
 * @author <a href="mailto:dogsong99@163.com">dosong</a>
 * @since 2025/8/15
 */
@Api(tags = "流程执行接口")
@RequestMapping("/api/flow")
@RestController
@Slf4j
public class FlowExecuteController {

    @Resource
    private FlowExecutor flowExecutor;

    @ApiOperation(value = "执行流程链", notes = "根据chainId执行对应的流程链")
    @PostMapping("/execute/{chainId}")
    @ResponseBody
    public CommonResult executeChain(
            @ApiParam(name = "chainId", value = "流程链ID", required = true)
            @PathVariable String chainId,
            @ApiParam(name = "requestData", value = "请求数据", required = false)
            @RequestBody(required = false) Object requestData) {
        
        log.info("开始执行流程链: {}, 请求数据: {}", chainId, requestData);
        
        try {
            LiteflowResponse response;
            if (requestData != null) {
                response = flowExecutor.execute2Resp(chainId, requestData, DataContext.class);
            } else {
                response = flowExecutor.execute2Resp(chainId, DataContext.class);
            }
            
            if (response.isSuccess()) {
                log.info("流程链 {} 执行成功", chainId);
                return CommonResult.builder()
                        .code("S")
                        .message("流程执行成功")
                        .build();
            } else {
                log.error("流程链 {} 执行失败: {}", chainId, response.getMessage());
                return CommonResult.builder()
                        .code("E")
                        .message("流程执行失败: " + response.getMessage())
                        .build();
            }
        } catch (Exception e) {
            log.error("执行流程链 {} 时发生异常", chainId, e);
            return CommonResult.builder()
                    .code("E")
                    .message("执行流程异常: " + e.getMessage())
                    .build();
        }
    }

    @ApiOperation(value = "测试流程执行", notes = "测试执行一个简单的流程")
    @PostMapping("/test")
    @ResponseBody
    public CommonResult testExecute() {
        log.info("开始测试流程执行");
        
        try {
            // 这里使用一个示例chainId进行测试，实际使用时应该根据业务情况修改
            LiteflowResponse response = flowExecutor.execute2Resp("chain1");
            
            if (response.isSuccess()) {
                log.info("测试流程执行成功");
                return CommonResult.builder()
                        .code("S")
                        .message("测试流程执行成功")
                        .build();
            } else {
                log.error("测试流程执行失败: {}", response.getMessage());
                return CommonResult.builder()
                        .code("E")
                        .message("测试流程执行失败: " + response.getMessage())
                        .build();
            }
        } catch (Exception e) {
            log.error("测试流程执行时发生异常", e);
            return CommonResult.builder()
                    .code("E")
                    .message("测试执行异常: " + e.getMessage())
                    .build();
        }
    }
}